﻿Imports System.Text

Public Class CreateQuestion
    Private type As Integer = -1
    Private fillAnswerCount As Integer = 0
    Private Sub cbType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbType.SelectedIndexChanged
        Select Case cbType.SelectedIndex
            Case 0
                lblShow.Text = "选择题"

                lblTitleExample.Text = "示例：关于季节"
                lblQueExample.Text = "示例：请问北京有多少个季节？"
                panelOptions.Visible = True
                gbSelectAnswer.Visible = True
                gbFillAnswer.Visible = False
                gbJudgeAnswer.Visible = False

                rtxSelectA.AllowEmpty = False
                rtxSelectB.AllowEmpty = False
                rtxSelectC.AllowEmpty = False
                rtxSelectD.AllowEmpty = False


                rtxFill1.AllowEmpty = True
                rtxFill2.AllowEmpty = True
                rtxFill3.AllowEmpty = True
                rtxFill4.AllowEmpty = True
                type = 0

            Case 1
                lblShow.Text = "判断题"
                panelOptions.Visible = False
                gbSelectAnswer.Visible = False
                gbJudgeAnswer.Visible = True
                gbFillAnswer.Visible = False
                lblTitleExample.Text = "示例：如何穿衣"
                lblQueExample.Text = "示例：判断下面的语句是否正确：北京的冬天不需要穿羽绒服。"
                type = 1

                rtxSelectA.AllowEmpty = True
                rtxSelectB.AllowEmpty = True
                rtxSelectC.AllowEmpty = True
                rtxSelectD.AllowEmpty = True


                rtxFill1.AllowEmpty = True
                rtxFill2.AllowEmpty = True
                rtxFill3.AllowEmpty = True
                rtxFill4.AllowEmpty = True
            Case 2
                lblShow.Text = "填空题"
                panelOptions.Visible = False
                gbSelectAnswer.Visible = False
                gbJudgeAnswer.Visible = False
                gbFillAnswer.Visible = True
                lblTitleExample.Text = "示例：雾霾"
                lblQueExample.Text = "示例：中国首都叫[[]]，它的雾霾指数[[]]经常非常高。每一个需要填空的地方要加上‘[[]]’,每个填空题里面不能超过四个空（可以少于四个）。"
                type = 2

                rtxSelectA.AllowEmpty = True
                rtxSelectB.AllowEmpty = True
                rtxSelectC.AllowEmpty = True
                rtxSelectD.AllowEmpty = True


        End Select
    End Sub

    Private Sub clearAll()
        rtxTitle.Text = ""
        rtxSelectA.Text = ""
        rtxSelectB.Text = ""
        rtxSelectC.Text = ""
        rtxSelectD.Text = ""
        rtxQuestion.Text = ""
        fillAnswerCount = 0
        rtxFill1.Text = ""
        rtxFill2.Text = ""
        rtxFill3.Text = ""
        rtxFill4.Text = ""
        rtxFill1.AllowEmpty = True
        rtxFill2.AllowEmpty = True
        rtxFill3.AllowEmpty = True
        rtxFill4.AllowEmpty = True
        rbA.Checked = True
        rbCorrect.Checked = True
    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        clearAll()
    End Sub


    Private Function buildOptions() As String
        Dim options As StringBuilder = New StringBuilder()
        options.Append(rtxSelectA.Text)
        options.Append("||")
        options.Append(rtxSelectB.Text)
        options.Append("||")
        options.Append(rtxSelectC.Text)
        options.Append("||")
        options.Append(rtxSelectD.Text)
        Return options.ToString()
    End Function

    Private Function getSelectAnswer() As String
        Dim answer As String = "A"
        If rbA.Checked Then
            answer = "A"
        ElseIf rbB.Checked Then
            answer = "B"
        ElseIf rbC.Checked Then
            answer = "C"
        ElseIf rbD.Checked Then
            answer = "D"
        End If
        Return answer
    End Function

    Private Function getJudgeAnswer() As String
        Dim answer As String = "1"
        If rbCorrect.Checked Then
            answer = "1"
        ElseIf rbWrong.Checked Then
            answer = "0"
        End If
        Return answer
    End Function

    Private Function getFillAnswer() As String
        Dim answer As StringBuilder = New StringBuilder()
        If fillAnswerCount > 0 Then
            answer.Append(rtxFill1.Text)
        End If
        If fillAnswerCount > 1 Then
            answer.Append("||")
            answer.Append(rtxFill2.Text)
        End If
        If fillAnswerCount > 2 Then
            answer.Append("||")
            answer.Append(rtxFill3.Text)
        End If
        If fillAnswerCount > 3 Then
            answer.Append("||")
            answer.Append(rtxFill4.Text)
        End If
        Return answer.ToString()
    End Function

    Private Function validateFillAnswer(que As String) As Boolean
        If fillAnswerCount < 1 Then
            fillAnswerCount = getFillCount(que)
            If fillAnswerCount < 1 Then
                MessageBox.Show("填空题至少要有一个空")
                Return False
            End If
        End If
        If fillAnswerCount > 4 Then
            MessageBox.Show("填空题不能多于4个空")
            Return False
        End If
        Return True
    End Function

    Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
        Dim que As String = rtxQuestion.Text

        Dim queType As Short = 0
        Dim right_answer As String = ""
        Dim options As String = ""
        Select Case cbType.SelectedIndex
            Case 0
                queType = 0
                options = buildOptions()
                right_answer = getSelectAnswer()
            Case 1
                queType = 1
                right_answer = getJudgeAnswer()
            Case 2
                queType = 2
                If Not validateFillAnswer(que) Then
                    Return
                End If
                right_answer = getFillAnswer()
                options = fillAnswerCount.ToString()
        End Select


        Dim title As String = rtxTitle.Text

        Try
            Me.QuestionTableAdapter1.InsertQuestion(que, queType, right_answer, title, options)
            MessageBox.Show("提交成功")
            clearAll()
            Me.Close()
        Catch ex As Exception
            MessageBox.Show("提交失败，" + ex.Message)
        End Try
    End Sub

    Private Function getFillCount(que As String) As Integer
        Dim count As Integer = 0
        While que.Contains("[[]]")
            Dim start As Integer = InStr(que, "[[]]")
            que = que.Remove(start - 1, 4)
            count = count + 1
        End While
        Return count
    End Function

    Private Sub CreateQuestion_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        cbType.SelectedIndex = 0
    End Sub

    Private Sub rtxQuestion_TextChanged(sender As Object, e As EventArgs) Handles rtxQuestion.TextChanged
        If type = 2 Then
            Dim que As String = rtxQuestion.Text
            fillAnswerCount = getFillCount(que)
            rtxFill1.AllowEmpty = True
            rtxFill2.AllowEmpty = True
            rtxFill3.AllowEmpty = True
            rtxFill4.AllowEmpty = True
            If fillAnswerCount > 0 Then
                rtxFill1.AllowEmpty = False
            End If
            If fillAnswerCount > 1 Then
                rtxFill2.AllowEmpty = False
            End If
            If fillAnswerCount > 2 Then
                rtxFill3.AllowEmpty = False
            End If
            If fillAnswerCount > 3 Then
                rtxFill4.AllowEmpty = False
            End If
        End If
    End Sub
End Class